/*
 * This file is part of Jeve.

    Jeve is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    Jeve is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    Copyright 2007 Jochen Bedersdorfer (beders@yahoo.com)
 *
 */
package de.monoid.eve.notifier;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Properties;

/**
 *  Notifier which uses the sms77.de website to send text messages to a cell phone.
 *  TODO document the properties
 * @author beders
 *
 */
public class Sms77Notifier implements Notifier {

  public void notify(Date aNotificationTime, String aMessage, Properties p) {
    String leadTime = p.getProperty("sms77.leadtime", "15"); // time is in minutes
    long leadTimeMsec = Integer.parseInt(leadTime) * 60 * 1000;
    long rest = aNotificationTime.getTime() - System.currentTimeMillis(); 
    if (rest >= 0 && rest < leadTimeMsec) {
      String user = p.getProperty("sms77.user");      
      String pwdHash = p.getProperty("sms77.passwd-hash");
      String to = p.getProperty("sms77.to");
      boolean test = Boolean.parseBoolean(p.getProperty("sms77.test", "false"));
      // it's time to notify        
      try {

        URL url = new URL("http://gateway.sms77.de/?u=" + URLEncoder.encode(user, "UTF-8") +
            "&p=" + URLEncoder.encode(pwdHash, "UTF-8") +
            "&to=" + URLEncoder.encode(to, "UTF-8") + 
            "&text=" + URLEncoder.encode(aMessage, "UTF-8") +
            "&type=basicplus" + ((test) ? "&debug=1" : "") +
            "&delay=" + aNotificationTime.getTime());
        System.err.println("Contacting: "+ url);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        int responseCode = connection.getResponseCode();
        System.err.println("Response code: " + responseCode);
        System.err.println("Content-Type:" + connection.getContentType());
        Object content = connection.getContent();
        System.err.println("Content:" + content + " of " + content.getClass());
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "iso-8859-1"));
        String result = reader.readLine();
        System.err.println("Result:" + result);
        int resultCode = Integer.parseInt(result);
        reader.close();
        switch (resultCode) {
        case 100:
          System.out.println("SMS sent!" + ((test) ? " NOT :) Just testing" : ""));
          break;
        case 500:
          System.out.println("Not enough money on the account:" + user);
          break;
          default:
            System.out.println("See Sms77 API doc for the return code.");
        }
      } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    } else {
      if (rest >= 0) { 
        System.out.println("Skill notification only within the last " + leadTime + " minutes. Skill training ends at " + aNotificationTime);
      } else {
        System.out.println("Currently no skill training");
      }
    }
  }

}
